package com.xuecheng.manage_cms.web.controller;

import com.xuecheng.framework.web.BaseController;
import com.xuecheng.manage_cms.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import java.io.IOException;

@Controller
public class CmsPagePreviewController extends BaseController {

    private PageService pageService;

    @Autowired
    public CmsPagePreviewController(PageService pageService){
        this.pageService = pageService;
    }

    // 接收页面id，页面预览
    @GetMapping("cms/preview/{pageId}")
    public void preview(@PathVariable("pageId") String pageId) throws IOException {
        // 执行静态化
        String pageHtml = pageService.getPageHtml(pageId);
        // 通过response对象将内容输出
        ServletOutputStream stream = response.getOutputStream();
        // 由于Nginx先请求cms的课程预览功能得到html页面，再解析页面中的ssi标签
        // 这里必须保证cms页面预览返回的页面的Content-Type为text/html;charset=utf-8
        response.setHeader("Content-type","text/html;charset=utf-8");
        stream.write(pageHtml.getBytes("utf-8"));
    }
}
